package tech.xiaozai.exam.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.xiaozai.common.CommonPage;
import tech.xiaozai.exam.bo.ExamQuestionBO;
import tech.xiaozai.exam.mapper.ExamQuestionMapper;
import tech.xiaozai.exam.mapper.ExamSubjectMapper;
import tech.xiaozai.exam.mapstruct.ExamQuestionMapStruct;
import tech.xiaozai.exam.po.ExamQuestion;
import tech.xiaozai.exam.po.ExamQuestionExample;
import tech.xiaozai.exam.po.ExamSubject;

import javax.security.auth.Subject;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-10-07 10:58
 */
@Service
public class ExamQuestionServiceImpl implements ExamQuestionService {

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    @Autowired
    private ExamSubjectMapper examSubjectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(ExamQuestionBO question){
        ExamQuestion po = ExamQuestionMapStruct.me.toPo(question);
        examQuestionMapper.insertSelective(po);
    }

    @Override
    public CommonPage<ExamQuestionBO> page(Integer pageNum, Integer pageSize, ExamQuestion question) {
        PageHelper.startPage(pageNum,pageSize);
        ExamQuestionExample example = new ExamQuestionExample();
        ExamQuestionExample.Criteria criteria = example.createCriteria();
        if(question.getQuestionType()!=null){
            criteria.andQuestionTypeEqualTo(question.getQuestionType());
        }
        if(question.getQuestion()!=null){
            criteria.andQuestionLike("%"+question.getQuestion()+"%");
        }
        if(question.getAnswer()!=null){
            criteria.andAnswerLike("%"+question.getAnswer()+"%");
        }
        if(question.getSubjectId()!=null){
            criteria.andSubjectIdEqualTo(question.getSubjectId());
        }
        Page<ExamQuestion> page = (Page<ExamQuestion>) examQuestionMapper.selectByExample(example);

        return new CommonPage<ExamQuestionBO>().setTotal(page.getTotal())
                .setPageNum(page.getPageNum())
                .setPageSize(page.getPageSize())
                .setTotalPages(page.getPages())
                .setList(ExamQuestionMapStruct.me.toBos(page.getResult()));
    }

    @Override
    public ExamQuestionBO get(Long id) {
        ExamQuestion examQuestion = examQuestionMapper.selectByPrimaryKey(id);
        if(examQuestion!=null){
            return ExamQuestionMapStruct.me.toBo(examQuestion);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExamQuestionBO question) {
        ExamQuestion po = ExamQuestionMapStruct.me.toPo(question);
        examQuestionMapper.updateByPrimaryKeySelective(po);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        ExamQuestionExample example = new ExamQuestionExample();
        example.createCriteria().andIdIn(ids);
        examQuestionMapper.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<ExamQuestionBO> questions) {
        for(ExamQuestionBO question : questions) {
            ExamQuestion po = ExamQuestionMapStruct.me.toPo(question);
            examQuestionMapper.insertSelective(po);
        }
    }

    @Override
    public List<ExamSubject> getSubjects() {
        List<ExamSubject> subjects = examSubjectMapper.selectByExample(null);
        return subjects;
    }
}
